/*
 * CourseManageInterFrm.java
 *
 * Created on __DATE__, __TIME__
 */

package com.jakey.view;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.jakey.dao.CourseDao;
import com.jakey.model.Course;
import com.jakey.model.Selection;
import com.jakey.util.DbUtil;
import com.jakey.util.StringUtil;

import Message.Message;
import client.TheClient;
import gui.DarkButton;
import gui.LightTextField;
import gui.MyTable;
import user.User;

import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.SystemColor;

/**
 *
 * @author  __USER__
 */
public class CourseManageInterFrm extends javax.swing.JInternalFrame {

	CourseDao courseDao = new CourseDao();
	private int NumSelected;

	/** Creates new form CourseManageInterFrm */
	public CourseManageInterFrm() {
		getContentPane().setBackground(SystemColor.control);
		setPreferredSize(new Dimension(700, 433));
		initComponents();
		this.setLocation(200, 50);
		this.fillTable(new Course());

	}

	public CourseManageInterFrm(User user) {
		getContentPane().setBackground(SystemColor.control);
		setPreferredSize(new Dimension(700, 433));
		initComponents();
		this.setLocation(200, 50);
		this.fillTable(new Course());

	}


	private void resetValue() {
		this.courseIdTxt.setText("");
		this.courseNameTxt.setText("");
		this.courseTeacherTxt.setText("");
		this.courseTimeTxt.setText("");
		this.capacityTxt.setText("");
		this.numSelectedTxt.setText("");
	}

	private void fillTable(Course course) {
		DefaultTableModel dtm = (DefaultTableModel) courseTable.getModel();
		dtm.setRowCount(0);
		String tp="SHOW_READERS";
		Message message=new Message();
		message.setCheckCode(6);
		message.setType(tp);
		message.setAllembracing(course);
		client.TheClient client=new TheClient();
		Message received;
		try {
			received = client.sendAndReceive(message);
			Vector<Course> TbB = new Vector<Course>();
			TbB = received.getCsvecVector();
			for(int i=0;i<TbB.size();i++) {
				Course temp = new Course();
				Vector v = new Vector();
				temp = TbB.get(i);
				v.add(temp.getCourseId());
				v.add(temp.getCourseName());
				v.add(temp.getCourseTime());
				v.add(temp.getCourseTeacher());
				v.add(temp.getCapacity());
				v.add(temp.getNumSelected());
				dtm.addRow(v);

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		s_courseNameTxt = new LightTextField();
		jLabel2 = new javax.swing.JLabel();
		s_courseTimeTxt = new LightTextField();
		jLabel3 = new javax.swing.JLabel();
		s_courseTeacherTxt = new LightTextField();
		jb_search = new DarkButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		courseTable = new MyTable();
		jPanel2 = new javax.swing.JPanel();
		courseIdTxt = new LightTextField();
		jLabel4 = new javax.swing.JLabel();
		courseNameTxt = new LightTextField();
		jLabel5 = new javax.swing.JLabel();
		courseTimeTxt = new LightTextField();
		jLabel6 = new javax.swing.JLabel();
		courseTeacherTxt = new LightTextField();
		jLabel7 = new javax.swing.JLabel();
		capacityTxt = new LightTextField();
		jLabel8 = new javax.swing.JLabel();
		numSelectedTxt = new LightTextField();
		jLabel9 = new javax.swing.JLabel();
		jb_modify = new DarkButton();
		jb_delete = new DarkButton();

		setClosable(true);
		setIconifiable(true);
		setTitle("\u8bfe\u7a0b\u4fe1\u606f\u4fee\u6539");

		jPanel1.setBorder(javax.swing.BorderFactory
				.createTitledBorder("\u641c\u7d22\u6761\u4ef6"));

		jLabel1.setText("\u8bfe\u7a0b\u540d\u79f0");

		jLabel2.setText("\u4efb\u8bfe\u8001\u5e08");

		jLabel3.setText("\u4e0a\u8bfe\u65f6\u95f4");

		jb_search
				.setIcon(new javax.swing.ImageIcon(
						"D:\\Workspaces\\MyEclipse 8.5\\CourseSys\\src\\com\\jakey\\view\\image\\search.png")); // NOI18N
		jb_search.setText("\u67e5\u8be2");
		jb_search.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jb_searchActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1Layout.setHorizontalGroup(
			jPanel1Layout.createParallelGroup(Alignment.LEADING)
				.addGroup(jPanel1Layout.createSequentialGroup()
					.addContainerGap()
					.addComponent(jLabel1)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(s_courseNameTxt, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(jLabel3)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(s_courseTimeTxt, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(jLabel2)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(s_courseTeacherTxt, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(jb_search, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(35, Short.MAX_VALUE))
		);
		jPanel1Layout.setVerticalGroup(
			jPanel1Layout.createParallelGroup(Alignment.LEADING)
				.addGroup(jPanel1Layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
						.addComponent(s_courseNameTxt, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
						.addComponent(jLabel1)
						.addComponent(jLabel3)
						.addComponent(s_courseTimeTxt, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
						.addComponent(jLabel2)
						.addComponent(s_courseTeacherTxt, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
						.addComponent(jb_search, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		jPanel1.setLayout(jPanel1Layout);

		courseTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				}, new String[] { "课程编号", "课程名称", "上课时间", "任课老师", "课程容量",
						"已选人数" }) {
			boolean[] canEdit = new boolean[] { false, false, false, false,
					false, false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		courseTable.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent evt) {
				courseTableMousePressed(evt);
			}
		});
		jScrollPane1.setViewportView(courseTable);

		jPanel2.setBorder(javax.swing.BorderFactory
				.createTitledBorder("\u8868\u5355\u64cd\u4f5c"));

		courseIdTxt.setEditable(false);

		jLabel4.setText("\u8bfe\u7a0b\u7f16\u53f7");

		jLabel5.setText("\u8bfe\u7a0b\u540d\u79f0");

		jLabel6.setText("\u4e0a\u8bfe\u65f6\u95f4");

		jLabel7.setText("\u4efb\u8bfe\u8001\u5e08");

		jLabel8.setText("\u8bfe\u7a0b\u5bb9\u91cf");

		numSelectedTxt.setEditable(false);

		jLabel9.setText("\u5df2\u9009\u4eba\u6570");

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2Layout.setHorizontalGroup(
			jPanel2Layout.createParallelGroup(Alignment.LEADING)
				.addGroup(jPanel2Layout.createSequentialGroup()
					.addGap(21)
					.addGroup(jPanel2Layout.createParallelGroup(Alignment.TRAILING)
						.addComponent(jLabel7)
						.addComponent(jLabel4))
					.addGap(18)
					.addGroup(jPanel2Layout.createParallelGroup(Alignment.LEADING)
						.addComponent(courseTeacherTxt, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE)
						.addComponent(courseIdTxt, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))
					.addGap(10)
					.addGroup(jPanel2Layout.createParallelGroup(Alignment.TRAILING)
						.addComponent(jLabel8)
						.addComponent(jLabel5))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(jPanel2Layout.createParallelGroup(Alignment.LEADING)
						.addComponent(capacityTxt, GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE)
						.addComponent(courseNameTxt, 135, 135, 135))
					.addGap(18)
					.addGroup(jPanel2Layout.createParallelGroup(Alignment.TRAILING)
						.addComponent(jLabel9)
						.addComponent(jLabel6))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(jPanel2Layout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(numSelectedTxt)
						.addComponent(courseTimeTxt, GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
					.addContainerGap())
		);
		jPanel2Layout.setVerticalGroup(
			jPanel2Layout.createParallelGroup(Alignment.LEADING)
				.addGroup(jPanel2Layout.createSequentialGroup()
					.addGap(19)
					.addGroup(jPanel2Layout.createParallelGroup(Alignment.BASELINE)
						.addComponent(courseTimeTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(jLabel6)
						.addComponent(courseIdTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(jLabel4)
						.addComponent(jLabel5)
						.addComponent(courseNameTxt, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE))
					.addGap(32)
					.addGroup(jPanel2Layout.createParallelGroup(Alignment.BASELINE)
						.addComponent(numSelectedTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(jLabel9)
						.addComponent(capacityTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(jLabel8)
						.addComponent(courseTeacherTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(jLabel7))
					.addContainerGap(14, Short.MAX_VALUE))
		);
		jPanel2.setLayout(jPanel2Layout);

		jb_modify
				.setIcon(new javax.swing.ImageIcon(
						"D:\\Workspaces\\MyEclipse 8.5\\CourseSys\\src\\com\\jakey\\view\\image\\modify.png")); // NOI18N
		jb_modify.setText("\u4fee\u6539");
		jb_modify.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jb_modifyActionPerformed(evt);
			}
		});

		jb_delete
				.setIcon(new javax.swing.ImageIcon(
						"D:\\Workspaces\\MyEclipse 8.5\\CourseSys\\src\\com\\jakey\\view\\image\\delete.png")); // NOI18N
		jb_delete.setText("\u5220\u9664");
		jb_delete.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jb_deleteActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		layout.setHorizontalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGap(190)
					.addComponent(jb_modify, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 196, Short.MAX_VALUE)
					.addComponent(jb_delete, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
					.addGap(158))
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(layout.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(jPanel1, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
						.addComponent(jScrollPane1, Alignment.LEADING)
						.addComponent(jPanel2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addContainerGap(29, Short.MAX_VALUE))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGap(18)
					.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, 122, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGroup(layout.createParallelGroup(Alignment.BASELINE)
						.addComponent(jb_modify)
						.addComponent(jb_delete))
					.addGap(21))
		);
		getContentPane().setLayout(layout);

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void jb_modifyActionPerformed(java.awt.event.ActionEvent evt) {
		
//		int count=courseTable.getSelectedRow();
//		if(count!=-1) {
//			String a=courseTable.getValueAt(count, 0).toString();
//			String b=courseTable.getValueAt(count, 1).toString();
//			String c=courseTable.getValueAt(count, 2).toString();
//			String d=courseTable.getValueAt(count, 3).toString();
//			String e=courseTable.getValueAt(count, 4).toString();
//			String f=courseTable.getValueAt(count, 5).toString();
//			this.courseIdTxt.setText(a);
//			this.courseNameTxt.setText(b);
//			this.courseTimeTxt.setText(c);
//			this.courseTeacherTxt.setText(d);
//			this.capacityTxt.setText(e);
//			this.numSelectedTxt.setText(f);
//			
//		}
//		String a=courseTable.getValueAt(count, 0).toString();
//		String courseId = a;
//		if (StringUtil.isEmpty(courseId)) {
//			JOptionPane.showMessageDialog(this, "��ѡ��Ҫ�޸ĵļ�¼!");
//			return;
//		}
		String courseId = this.courseIdTxt.getText();
		String courseName = this.courseNameTxt.getText();
		String courseTime = this.courseTimeTxt.getText();
		String courseTeacher = this.courseTeacherTxt.getText();
		String capacity = this.capacityTxt.getText();
		System.out.println(capacity);
		if (StringUtil.isEmpty(courseName)) {
			JOptionPane.showMessageDialog(this, "课程名称不能为空！");
			return;
		}
		if (StringUtil.isEmpty(courseTime)) {
			JOptionPane.showMessageDialog(this, "上课时间不能为空！");
			return;
		}
		if (StringUtil.isEmpty(courseTeacher)) {
			JOptionPane.showMessageDialog(this, "任课老师不能为空！");
			return;
		}
		if (StringUtil.isEmpty(capacity)) {
			JOptionPane.showMessageDialog(this, "课程容量不能为空！");
			return;
		}
		if (Integer.parseInt(capacity) < NumSelected) {
			JOptionPane.showMessageDialog(this, "课程容量不能小于已选课程人数！");
			return;
		}
	
		Course course = new Course(Integer.parseInt(courseId), courseName,
				courseTime, courseTeacher, Integer.parseInt(capacity));
		Message message = new Message("COURSE_MODIFY", course);
		message.setCheckCode(6);
		client.TheClient client = new TheClient();
	
		Message recevied = new Message();
		try {
			recevied = client.sendAndReceive(message);
			if (recevied.isResponse()) {
				JOptionPane.showMessageDialog(this, "修改成功！");
				this.resetValue();
				this.fillTable(new Course());
				MyTable.colorizeTabel(courseTable);
			} else {
				JOptionPane.showMessageDialog(this, "修改失败！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, "修改失败！");
		} finally {
			try {
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void courseTableMousePressed(java.awt.event.MouseEvent evt) {
		//��ȡѡ�е���
		int count=courseTable.getSelectedRow();
		String a=courseTable.getValueAt(count, 0).toString();
		String b=courseTable.getValueAt(count, 1).toString();
		String c=courseTable.getValueAt(count, 2).toString();
		String d=courseTable.getValueAt(count, 3).toString();
		String e=courseTable.getValueAt(count, 4).toString();
		String f=courseTable.getValueAt(count, 5).toString();
		this.courseIdTxt.setText(a);
		this.courseNameTxt.setText(b);
		this.courseTimeTxt.setText(c);
		this.courseTeacherTxt.setText(d);
		this.capacityTxt.setText(e);
		this.numSelectedTxt.setText(f);
	}

	private void jb_searchActionPerformed(java.awt.event.ActionEvent evt) {
		String s_courseName = this.s_courseNameTxt.getText();
		String s_courseTime = this.s_courseTimeTxt.getText();
		String s_courseTeacher = this.s_courseTeacherTxt.getText();
		Course course = new Course(s_courseName, s_courseTime, s_courseTeacher);
		this.fillTable(course);

	}

	private void jb_deleteActionPerformed(java.awt.event.ActionEvent evt) {
//		int count=courseTable.getSelectedRow();
//		String courseId=courseTable.getValueAt(count, 0).toString();
		String courseId = this.courseIdTxt.getText();
		if (StringUtil.isEmpty(courseId)) {
			JOptionPane.showMessageDialog(this, "请选择要删除的记录！");
			return;
		}
		if (NumSelected > 0) {
			JOptionPane.showMessageDialog(this, "本课程已有人选，不能删除！");
			return;
		}
		int n = JOptionPane.showConfirmDialog(this, "确定要删除这条记录吗！");
		if (n == 0) {
			Message message = new Message();
			message.setS(courseId);
			message.setType("COURSE_DELETE");
			message.setCheckCode(6);
			client.TheClient client = new TheClient();
			
			Message recevied = new Message();
			try {
				recevied = client.sendAndReceive(message);
				if (recevied.isResponse()) {
					JOptionPane.showMessageDialog(this, "删除成功!");
					this.resetValue();
					this.fillTable(new Course());
				} else {
					JOptionPane.showMessageDialog(this, "删除失败!");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				JOptionPane.showMessageDialog(this, "删除失败!");
			} finally {
				try {
			
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private LightTextField capacityTxt;
	private LightTextField courseIdTxt;
	private LightTextField courseNameTxt;
	private javax.swing.JTable courseTable;
	private LightTextField courseTeacherTxt;
	private LightTextField courseTimeTxt;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JScrollPane jScrollPane1;
	private DarkButton jb_delete;
	private DarkButton jb_modify;
	private DarkButton jb_search;
	private LightTextField numSelectedTxt;
	private LightTextField s_courseNameTxt;
	private LightTextField s_courseTeacherTxt;
	private LightTextField s_courseTimeTxt;
	// End of variables declaration//GEN-END:variables

}